<template>
  <div>
      <a-modal title="入职申请" :width="width" :visible="visible" @ok="ok" @cancel="cancel">
        <template #footer>
          <a-button key="back" @click="cancel">关闭</a-button>
          <a-button key="submit" type="primary" @click="fnSaveInfo">保存</a-button>
          <a-button key="submit" type="primary" @click="fnSubmitApply">提交申请</a-button>
        </template>
        <a-table :dataSource="rows" :columns="columns" :pagination="false"
          :expandedRowKeys="expandedRowKeys"
          bordered @expandedRowsChange="expandedRowsChange">
          <template #expandedRowRender>
            <a-form-model ref="form" :form="rows[num].form" style="margin-top: 16px">
              <a-row>
                <a-col :span="6">
                  <a-form-item label="子单位" name="zidanwei">
                    <a-input placeholder="请输入子单位" v-model="rows[num].zidanwei" style="width:60%"></a-input>
                  </a-form-item>
                </a-col>
                <a-col :span="6">
                  <a-form-item label="增员原因" name="zyreason">
                    <a-select placeholder="请选择增员原因" v-model="rows[num].zyreason" style="width:60%">
                      <a-select-option value="调转">调转</a-select-option>
                      <a-select-option value="招聘">招聘</a-select-option>
                      <a-select-option value="其它">其它</a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="6">
                  <a-form-item label="增员时间" name="zydate">
                    <a-date-picker v-model="rows[num].zydate" />
                  </a-form-item>
                </a-col>
                <a-col :span="6">
                  <a-form-item label="起薪时间" name="qxdate">
                    <a-date-picker v-model="rows[num].qxdate" />
                  </a-form-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="24">
                  <a-form-item label="备注" name="remark">
                    <a-textarea placeholder="请输入备注" v-model="rows[num].remark"></a-textarea>
                  </a-form-item>
                </a-col>
              </a-row>
            </a-form-model>
          </template>
        </a-table>
      </a-modal>
      <!--提交申请表单-->
        <a-modal title="提交审批" v-model="modalVisible" :mask-closable="false" :width="500" :footer="null">
          <div v-if="modalVisible">
            <a-form-item label="选择审批人" v-show="showAssign">
              <a-select style="width: 100%"
                v-model="form.assignees"
                placeholder="请选择"
                :allowClear="true"
              >
              <a-select-option v-for="(item, i) in assigneeList" :key="i" :value="item.username">{{item.realname}}</a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="下一审批人" v-show="isGateway">
              <a-alert  type="info" showIcon message="分支网关处不支持自定义选择下一审批人，将自动下发给所有可审批人。">，将发送给下一节点所有人</a-alert>
            </a-form-item>
            <a-form-item label="优先级" prop="priority">
              <a-select v-model="form.priority" placeholder="请选择" :allowClear="true" style="width: 100%">
                <a-select-option :value="0">普通</a-select-option>
                <a-select-option :value="1">重要</a-select-option>
                <a-select-option :value="2">紧急</a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="消息通知">
              <a-checkbox v-model="form.sendMessage">站内消息通知</a-checkbox>
              <a-checkbox v-model="form.sendSms" disabled>短信通知</a-checkbox>
              <a-checkbox v-model="form.sendEmail" disabled>邮件通知</a-checkbox>
            </a-form-item>
            <div slot="footer">
              <a-button type="text" @click="cancelTijiao">取消</a-button>
              <div style="display:inline-block;width: 20px;"></div>
              <a-button type="primary" :disabled="submitLoading" @click="applySubmit">提交</a-button>
            </div>
          </div>
      </a-modal>
  </div>
</template>

<script>
const columns = [
  { dataIndex: 'id', key: 'id', title: 'ID' },
  { dataIndex: 'name', key: 'name', title: '姓名' },
  { dataIndex: 'borth', key: 'borth', title: '生日' },
  { dataIndex: 'sfzh', key: 'sfzh', title: '身份证' },
]
import { addRuzhi,postAction } from '@/api/manage'
import { axios } from '@/utils/request'

export default {
  props: {
    visible: Boolean,
    rows: [],
  },
  data() {
    return {
      columns,
      form:{
          priority:0,
          assignees:[],
          sendMessage:true
        },
      num: 0,
      width: 1400,
      visible: false,
      expandedRowKeys:[],
      modalVisible:false,
      assigneeList:[],
      submitLoading: false,
      url:{
        getFirstNode:'/actProcessIns/getFirstNode',
          applyBusiness:'/actBusiness/apply'
      }
    }
  },
  created() {
    console.log("jieshou:"+JSON.stringify(this.rows))
    this.rows=this.rows.map(item=>{
      return item
    })
  },
  methods: {
    ok() {
      var count = 0
      this.rows.forEach((item,index,array)=>{
          if(item.zidanwei == '' || item.zidanwei == null
           || item.zyreason == '' || item.zyreason == null
            || item.zydate == '' || item.zydate == null
             || item.qxdate == '' || item.qxdate == null){
            this.$message.error('请补全' + item.name + '信息')
            count++
          }
      })
      if(count == 0){
        this.$emit('ok',this.rows)
      }
    },
    //保存数据
    fnSaveInfo(){
      var count = 0
      this.rows.forEach((item,index,array)=>{
        if(item.zidanwei == '' || item.zidanwei == null
          || item.zyreason == '' || item.zyreason == null
          || item.zydate == '' || item.zydate == null
          || item.qxdate == '' || item.qxdate == null){
          this.$message.error('请补全' + item.name + '信息')
          count++
        }
      }) 
      if(count == 0){
        console.log("this.rows:"+JSON.stringify(this.rows))
        // this.axios({
        //   url: '/bj/bjIdcardfk/addRuzhi',
        //   method:'post' ,
        //   data: this.rows
        // })

        addRuzhi(this.rows).then(res=>{
           // console.log("保存:"+JSON.stringify(res))
            const result=res.result
            this.rows.forEach((item)=>{
                result.forEach((it)=>{
                  
                  if(it.sfzh==item.sfzh){
                      item.ruzhiid=it.ruzhiid
                  }
                })
            })
            //console.log("保存:"+JSON.stringify(this.rows))
        })
         this.$message.success("保存成功");
        //this.$emit('fnSaveInfo',this.rows)
      }
    },
    applySubmit() {
                if (this.showAssign && this.form.assignees.length < 1) {
                  this.error = "请至少选择一个审批人";
                  this.$message.error(this.error)
                  return;
                } else {
                  this.error = "";
                }
                this.submitLoading = true;
                var params = Object.assign({},this.form);
               // params.assignees = params.assignees.join(",")
                let that = this;
                this.postFormAction(this.url.applyBusiness,params).then(res => {
                  if (res.success) {
                    this.$message.success("操作成功");
                    //that.loadData();
                    that.modalVisible = false;
                    that.cancel()
                  }else {
                    this.$message.error(res.message)
                  }
                }).finally(()=>this.submitLoading = false);
             },
    //提交申请
    fnSubmitApply(){
      const ids=this.rows.map(item=>{
        return item.ruzhiid
      })
       
        var params={
          type:1,
          ids:ids.toString()
        }
      postAction('/bj/bjIdcardfk/saveActBussinessDataBeforApprove', params).then((res) => {
            console.log("提审:"+JSON.stringify(res))
             if (res.success) {
                this.apply(res.result);
             } else {
               this.$message.error('失败')
             }
        })
    },
    apply(v) {
              if (!v.procDefId || v.procDefId == "null") {
                this.$message.error("流程定义为空");
                return;
              }
              this.form.id = v.id;
              this.form.procDefId = v.procDefId;
              this.form.title = v.title;
              // 加载审批人
              this.getAction(this.url.getFirstNode,{procDefId:v.procDefId}).then(res => {
                if (res.success) {
                  if (res.result.type == 3 || res.result.type == 4) {
                    this.isGateway = true;
                    this.modalVisible = true;
                    this.form.firstGateway = true;
                    this.showAssign = false;
                    this.error = "";
                    return;
                  }
                  this.form.firstGateway = false;
                  this.isGateway = false;
                  if (res.result.users && res.result.users.length > 0) {
                    this.error = "";
                    this.assigneeList = res.result.users;
                    // 默认勾选
                   // let ids = [];
                   // res.result.users.forEach(e => {
                   //   ids.push(e.username);
                   // });
                    //this.form.assignees = ids;
                    this.form.assignees = [];
                    this.showAssign = true;
                  } else {
                    this.form.assignees = [];
                    this.showAssign = true;
                    this.error = '审批节点未分配候选审批人员，请联系管理员！';
                  }
                  if (this.error){
                    this.$message.error(this.error)
                    return;
                  }
                  this.modalVisible = true;
                }else {
                  this.$message.error(res.message)
                }
              });

            },
            cancelTijiao(){
               var id=this.form.id;
                postAction('/bj/bjIdcardfk/deleteActBussinessById', { actBussinessid: id }).then((res) => {
                    if (res.success) {
                       this.modalVisible = false;
                    } else {
                      this.$message.error('失败')
                    }
                 })
             },
    cancel() {
      this.$emit('RZC','')
      this.$emit('close')
    },
    expandedRowsChange(expandedRows){
      var index=expandedRows[expandedRows.length-1]
      this.expandedRowKeys=[index];
      this.num=index;
    }
  },
}
</script>

<style>
</style>